Completed
Branch master (93a1f2)
by Daniel
01:17
created

utils.test.js ➔ ... ➔ ???   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 124
Bugs 0 Features 32
Metric Value
cc 1
nc 1
dl 0
loc 13
rs 9.4285
c 124
b 0
f 32
nop 0
1
import chai from 'chai';
2
import {isString, trim, removeSpaces, replace, removeNonWords, append,
3
    at, between, chars, collapseWhitespace, contains, containsAll, containsAny, countSubstr,
4
    endsWith, startsWith, ensureLeft, ensureRight, first, last, indexOf, lastIndexOf, insert,
5
    length, leftPad, rightPad, prepend, removeLeft, appendArray, prependArray, removeRight,
6
    repeat, reverse, shuffle, surround, safeTruncate, transliterate, truncate, removeEmptyStrings,
7
    format, compare, equal, inequal, hexEncode, hexDecode, binEncode, binDecode, decEncode,
8
    decDecode, base64Encode, base64Decode, htmlDecode, htmlEncode}
9
    from '../src/strman';
10
11
describe('isString function', () => {
12
    it('should be false', () => {
13
        let fixtures = [
14
            1,
15
            false,
16
            1.2,
17
            [],
18
            {}
19
        ];
20
21
        fixtures.forEach(el => {
22
            chai.expect(isString(el)).to.equal(false);
23
        });
24
    });
25
    it('should be true', () => {
26
        let fixtures = [
27
            'string',
28
            'string'
29
        ];
30
31
        fixtures.forEach(el => {
32
            chai.expect(isString(el)).to.equal(true);
33
        });
34
    });
35
});
36
37
describe('trim, ltrim and rtrim function', () => {
38
    it('should be foo bar', () => {
39
        let fixtures = [
40
            'foo bar',
41
            'foo bar ',
42
            ' foo bar',
43
            '  foo bar   '
44
        ];
45
46
        fixtures.forEach(el => {
47
            chai.expect(trim(el)).to.equal('foo bar');
48
        });
49
    });
50
    it('should be foo bar without @', () => {
51
        let fixtures = [
52
            'foo bar',
53
            'foo bar@',
54
            '@foo bar',
55
            '@@foo bar@@@'
56
        ];
57
58
        fixtures.forEach(el => {
59
            chai.expect(trim(el, '@')).to.equal('foo bar');
60
        });
61
    });
62
    it('should be foo bar without @ and with #', () => {
63
        let fixtures = [
64
            '@#foo bar',
65
            '#foo bar@',
66
            '@#foo bar@',
67
            '@@#foo bar@@@'
68
        ];
69
70
        fixtures.forEach(el => {
71
            chai.expect(trim(el, '@')).to.equal('#foo bar');
72
        });
73
    });
74
});
75
76
describe('removeSpaces function', () => {
77
    it('should be foobar', () => {
78
        let fixtures = [
79
            'foo bar',
80
            'foo bar ',
81
            ' foo bar',
82
            ' foo bar '
83
        ];
84
85
        fixtures.forEach(el => {
86
            chai.expect(removeSpaces(el)).to.equal('foobar');
87
        });
88
    });
89
    it('should be foo-bar', () => {
90
        let fixtures = [
91
            'foo bar'
92
        ];
93
94
        fixtures.forEach(el => {
95
            chai.expect(removeSpaces(el, '-')).to.equal('foo-bar');
96
        });
97
    });
98
});
99
100
describe('replace function', () => {
101
    it('should be bar bar', () => {
102
        let fixtures = [
103
            'foo bar'
104
        ];
105
106
        fixtures.forEach(el => {
107
            chai.expect(replace(el, 'foo', 'bar')).to.equal('bar bar');
108
        });
109
    });
110
    it('should be bar bar bar', () => {
111
        let fixtures = [
112
            'foo bar foo'
113
        ];
114
115
        fixtures.forEach(el => {
116
            chai.expect(replace(el, 'foo', 'bar')).to.equal('bar bar bar');
117
        });
118
    });
119
});
120
121
describe('replace function caseSensitive', () => {
122
    it('should be bar bar', () => {
123
        let fixtures = [
124
            'FOO bar'
125
        ];
126
127
        fixtures.forEach(el => {
128
            chai.expect(replace(el, 'foo', 'bar', false)).to.equal('bar bar');
129
        });
130
    });
131
    it('should be bar bar bar', () => {
132
        let fixtures = [
133
            'FOO bar foo'
134
        ];
135
136
        fixtures.forEach(el => {
137
            chai.expect(replace(el, 'foo', 'bar', false)).to.equal('bar bar bar');
138
        });
139
    });
140
});
141
142
describe('transliterate function', () => {
143
    it('should be foo bar', () => {
144
        let fixtures = [
145
            'fóõ bár'
146
        ];
147
148
        fixtures.forEach(el => {
149
            chai.expect(transliterate(el)).to.equal('foo bar');
150
        });
151
    });
152
});
153
154
describe('append function', () => {
155
    it('should be foobar', () => {
156
        chai.expect(append('f', 'o', 'o', 'b', 'a', 'r')).to.equal('foobar');
157
        chai.expect(append('foobar')).to.equal('foobar');
158
        chai.expect(append('', 'foobar')).to.equal('foobar');
159
    });
160
    it('should be throw', () => {
161
        chai.assert.throws(append.bind(this, '', 1), Error);
162
        chai.assert.throws(append.bind(this, '', true), Error);
163
        chai.assert.throws(append.bind(this, '', false), Error);
164
        chai.assert.throws(append.bind(this, '', 1.2), Error);
165
        chai.assert.throws(append.bind(this, '', {}), Error);
166
        chai.assert.throws(append.bind(this, '', []), Error);
167
    });
168
});
169
170
describe('appendArray function', () => {
171
    it('should be foobar', () => {
172
        chai.expect(appendArray('f', ['o', 'o', 'b', 'a', 'r'])).to.equal('foobar');
173
        chai.expect(appendArray('foobar')).to.equal('foobar');
174
        chai.expect(appendArray('', ['foobar'])).to.equal('foobar');
175
    });
176
    it('should be throw', () => {
177
        chai.assert.throws(appendArray.bind(this, '', 1), Error);
178
        chai.assert.throws(appendArray.bind(this, '', true), Error);
179
        chai.assert.throws(appendArray.bind(this, '', false), Error);
180
        chai.assert.throws(appendArray.bind(this, '', 1.2), Error);
181
        chai.assert.throws(appendArray.bind(this, '', {}), Error);
182
        chai.assert.throws(appendArray.bind(this, '', [1]), Error);
183
        chai.assert.throws(appendArray.bind(this, '', [true]), Error);
184
        chai.assert.throws(appendArray.bind(this, '', [false]), Error);
185
        chai.assert.throws(appendArray.bind(this, '', [1.2]), Error);
186
        chai.assert.throws(appendArray.bind(this, '', [{}]), Error);
187
    });
188
});
189
190
describe('prepend function', () => {
191
    it('should be foobar', () => {
192
        chai.expect(prepend('r', 'f', 'o', 'o', 'b', 'a')).to.equal('foobar');
193
        chai.expect(prepend('foobar')).to.equal('foobar');
194
        chai.expect(prepend('', 'foobar')).to.equal('foobar');
195
        chai.expect(prepend('bar', 'foo')).to.equal('foobar');
196
    });
197
    it('should be throw', () => {
198
        chai.assert.throws(prepend.bind(this, '', 1), Error);
199
        chai.assert.throws(prepend.bind(this, '', []), Error);
200
        chai.assert.throws(prepend.bind(this, '', true), Error);
201
        chai.assert.throws(prepend.bind(this, '', false), Error);
202
        chai.assert.throws(prepend.bind(this, '', 1.2), Error);
203
        chai.assert.throws(prepend.bind(this, '', {}), Error);
204
    });
205
});
206
207
describe('prependArray function', () => {
208
    it('should be foobar', () => {
209
        chai.expect(prependArray('r', ['f', 'o', 'o', 'b', 'a'])).to.equal('foobar');
210
        chai.expect(prependArray('foobar')).to.equal('foobar');
211
        chai.expect(prependArray('', ['foobar'])).to.equal('foobar');
212
        chai.expect(prependArray('bar', ['foo'])).to.equal('foobar');
213
    });
214
    it('should be throw', () => {
215
        chai.assert.throws(prependArray.bind(this, '', 1), Error);
216
        chai.assert.throws(prependArray.bind(this, '', true), Error);
217
        chai.assert.throws(prependArray.bind(this, '', false), Error);
218
        chai.assert.throws(prependArray.bind(this, '', 1.2), Error);
219
        chai.assert.throws(prependArray.bind(this, '', {}), Error);
220
        chai.assert.throws(prependArray.bind(this, '', [1]), Error);
221
        chai.assert.throws(prependArray.bind(this, '', [true]), Error);
222
        chai.assert.throws(prependArray.bind(this, '', [false]), Error);
223
        chai.assert.throws(prependArray.bind(this, '', [1.2]), Error);
224
        chai.assert.throws(prependArray.bind(this, '', [{}]), Error);
225
    });
226
});
227
228
describe('at function', () => {
229
    it('should be f', () => {
230
        chai.expect(at('foobar', 0)).to.equal('f');
231
        chai.expect(at('ofobar', 1)).to.equal('f');
232
        chai.expect(at('oobarf', -1)).to.equal('f');
233
        chai.expect(at('oobafr', -2)).to.equal('f');
234
235
    });
236
    it('should be throw', () => {
237
        chai.assert.throws(at.bind(this, 1, 1), Error);
238
        chai.assert.throws(at.bind(this, [], 1), Error);
239
        chai.assert.throws(at.bind(this, true, 1), Error);
240
        chai.assert.throws(at.bind(this, false, 1), Error);
241
        chai.assert.throws(at.bind(this, 1.2, 1), Error);
242
        chai.assert.throws(at.bind(this, {}, 1), Error);
243
        chai.assert.throws(at.bind(this, '', ''), Error);
244
        chai.assert.throws(at.bind(this, '', []), Error);
245
        chai.assert.throws(at.bind(this, '', true), Error);
246
        chai.assert.throws(at.bind(this, '', false), Error);
247
        chai.assert.throws(at.bind(this, '', {}), Error);
248
    });
249
});
250
251
describe('between function', () => {
252
    it('should be ["foo"]', () => {
253
        chai.expect(between('[foo]', '[', ']')[0]).to.equal('foo');
254
        chai.expect(between('<span>foo</span>', '<span>', '</span>')[0]).to.equal('foo');
255
        chai.expect(between('<span>bar</span><span>foo</span>', '<span>', '</span>')[0]).to.equal('bar');
256
        chai.expect(between('<span>bar</span><span>foo</span>', '<span>', '</span>')[1]).to.equal('foo');
257
    });
258
    it('should be throw', () => {
259
        chai.assert.throws(between.bind(this, '', '', 1), Error);
260
        chai.assert.throws(between.bind(this, '', '', []), Error);
261
        chai.assert.throws(between.bind(this, '', '', {}), Error);
262
        chai.assert.throws(between.bind(this, '', '', true), Error);
263
        chai.assert.throws(between.bind(this, '', '', false), Error);
264
        chai.assert.throws(between.bind(this, '', '', 1.2), Error);
265
        chai.assert.throws(between.bind(this, '', 1, ''), Error);
266
        chai.assert.throws(between.bind(this, '', [], ''), Error);
267
        chai.assert.throws(between.bind(this, '', {}, ''), Error);
268
        chai.assert.throws(between.bind(this, '', true, ''), Error);
269
        chai.assert.throws(between.bind(this, '', false, ''), Error);
270
        chai.assert.throws(between.bind(this, '', 1.2, ''), Error);
271
        chai.assert.throws(between.bind(this, 1, '', ''), Error);
272
        chai.assert.throws(between.bind(this, [], '', ''), Error);
273
        chai.assert.throws(between.bind(this, {}, '', ''), Error);
274
        chai.assert.throws(between.bind(this, true, '', ''), Error);
275
        chai.assert.throws(between.bind(this, false, '', ''), Error);
276
        chai.assert.throws(between.bind(this, 1.2, '', ''), Error);
277
    });
278
});
279
280
describe('chars function', () => {
281
    it('should be ["t", "i", "t", "l", "e"]', () => {
282
        let title = 'title';
283
        chai.expect(chars(title)[0]).to.equal('t');
284
        chai.expect(chars(title)[1]).to.equal('i');
285
        chai.expect(chars(title)[2]).to.equal('t');
286
        chai.expect(chars(title)[3]).to.equal('l');
287
        chai.expect(chars(title)[4]).to.equal('e');
288
    });
289
    it('should be throw', () => {
290
        chai.assert.throws(chars.bind(this, 1), Error);
291
        chai.assert.throws(chars.bind(this, []), Error);
292
        chai.assert.throws(chars.bind(this, {}), Error);
293
        chai.assert.throws(chars.bind(this, true), Error);
294
        chai.assert.throws(chars.bind(this, false), Error);
295
        chai.assert.throws(chars.bind(this, 1.2), Error);
296
    });
297
});
298
299
describe('collapseWhitespace function', () => {
300
    it('should be foo bar', () => {
301
        let fixtures = [
302
            'foo    bar',
303
            '     foo     bar    ',
304
            ' foo     bar   ',
305
            '    foo     bar '
306
        ];
307
308
        fixtures.forEach(el => {
309
            chai.expect(collapseWhitespace(el)).to.equal('foo bar');
310
        });
311
    });
312
});
313
314
describe('removeNonWords function', () => {
315
    it('should be foobar', () => {
316
        let fixtures = [
317
            'foo bar',
318
            'foo&bar-'
319
        ];
320
321
        fixtures.forEach(el => {
322
            chai.expect(removeNonWords(el)).to.equal('foobar');
323
        });
324
    });
325
});
326
327
describe('contains function', () => {
328
    it('should be true, caseSensitive = true', () => {
329
        let fixtures = [
330
            'foo bar',
331
            'bar foo',
332
            'foobar',
333
            'foo'
334
        ];
335
336
        fixtures.forEach(el => {
337
            chai.expect(contains(el, 'foo', true)).to.equal(true);
338
        });
339
    });
340
341
    it('should be true, caseSensitive = false', () => {
342
        let fixtures = [
343
            'foo bar',
344
            'bar foo',
345
            'foobar',
346
            'foo'
347
        ];
348
349
        fixtures.forEach(el => {
350
            chai.expect(contains(el, 'FOO', false)).to.equal(true);
351
        });
352
    });
353
354
    it('should be false, caseSensitive = true', () => {
355
        let fixtures = [
356
            'foo bar',
357
            'bar foo',
358
            'foobar',
359
            'foo'
360
        ];
361
362
        fixtures.forEach(el => {
363
            chai.expect(contains(el, 'FOO', true)).to.equal(false);
364
        });
365
    });
366
367
    it('should be false, caseSensitive = false', () => {
368
        let fixtures = [
369
            'foo bar',
370
            'bar foo',
371
            'foobar',
372
            'foo'
373
        ];
374
375
        fixtures.forEach(el => {
376
            chai.expect(contains(el, 'dleitee', false)).to.equal(false);
377
        });
378
    });
379
});
380
381
describe('containsAll function', () => {
382
    it('should be false, caseSensitive = true', () => {
383
        let fixtures = [
384
            'foo bar',
385
            'bar foo',
386
            'foobar'
387
        ];
388
389
        fixtures.forEach(el => {
390
            chai.expect(containsAll(el, [], true)).to.equal(false);
391
        });
392
    });
393
394
    it('should be true, caseSensitive = true', () => {
395
        let fixtures = [
396
            'foo bar',
397
            'bar foo',
398
            'foobar'
399
        ];
400
401
        fixtures.forEach(el => {
402
            chai.expect(containsAll(el, ['foo', 'bar'], true)).to.equal(true);
403
        });
404
    });
405
406
    it('should be true, caseSensitive = false', () => {
407
        let fixtures = [
408
            'foo bar',
409
            'bar foo',
410
            'foobar'
411
        ];
412
413
        fixtures.forEach(el => {
414
            chai.expect(containsAll(el, ['FOO', 'BAR'], false)).to.equal(true);
415
        });
416
    });
417
418
    it('should be false, caseSensitive = true', () => {
419
        let fixtures = [
420
            'foo',
421
            'bar foo',
422
            'foobar',
423
            'foo'
424
        ];
425
426
        fixtures.forEach(el => {
427
            chai.expect(containsAll(el, ['FOO', 'BAR'], true)).to.equal(false);
428
        });
429
    });
430
431
    it('should be false, caseSensitive = false', () => {
432
        let fixtures = [
433
            'foo bar',
434
            'bar foo',
435
            'foobar',
436
            'foo'
437
        ];
438
439
        fixtures.forEach(el => {
440
            chai.expect(containsAll(el, ['foo', 'bar', 'dleitee'], false)).to.equal(false);
441
        });
442
    });
443
});
444
445
446
describe('containsAny function', () => {
447
    it('should be true, caseSensitive = true', () => {
448
        let fixtures = [
449
            'foo bar',
450
            'bar foo',
451
            'foobar'
452
        ];
453
454
        fixtures.forEach(el => {
455
            chai.expect(containsAny(el, ['foo', 'bar', 'test'], true)).to.equal(true);
456
        });
457
    });
458
459
    it('should be true, caseSensitive = false', () => {
460
        let fixtures = [
461
            'foo bar',
462
            'bar foo',
463
            'foobar'
464
        ];
465
466
        fixtures.forEach(el => {
467
            chai.expect(containsAny(el, ['FOO', 'BAR', 'Test'], false)).to.equal(true);
468
        });
469
    });
470
471
    it('should be false, caseSensitive = true', () => {
472
        let fixtures = [
473
            'foo',
474
            'bar foo',
475
            'foobar',
476
            'foo'
477
        ];
478
479
        fixtures.forEach(el => {
480
            chai.expect(containsAny(el, ['FOO', 'BAR', 'TEST'], true)).to.equal(false);
481
        });
482
    });
483
484
    it('should be false, caseSensitive = false', () => {
485
        let fixtures = [
486
            'foo bar',
487
            'bar foo',
488
            'foobar',
489
            'foo'
490
        ];
491
492
        fixtures.forEach(el => {
493
            chai.expect(containsAny(el, ['dleitee'], false)).to.equal(false);
494
        });
495
    });
496
});
497
498
describe('countSubstr function', () => {
499
    it('should be 7', () => {
500
        let fixtures = [
501
            'aaaaaAaaAA',
502
            'faaaAAaaaaAA',
503
            'aaAAaaaaafA',
504
            'AAaaafaaaaAAAA'
505
        ];
506
507
        fixtures.forEach(el => {
508
            chai.expect(countSubstr(el, 'a')).to.equal(7);
509
        });
510
    });
511
512
    it('should be 7 without caseSensitive', () => {
513
        let fixtures = [
514
            'aaaaaaa',
515
            'faaaaaaa',
516
            'aaaaaaaf',
517
            'aaafaaaa'
518
        ];
519
520
        fixtures.forEach(el => {
521
            chai.expect(countSubstr(el, 'A', false)).to.equal(7);
522
        });
523
    });
524
525
526
    it('should be 2 with allowOverlaping', () => {
527
        chai.expect(countSubstr('aaa', 'aa', true, true)).to.equal(2);
528
    });
529
530
    it('should be 1 without allowOverlaping', () => {
531
        chai.expect(countSubstr('aaa', 'aa', true, false)).to.equal(1);
532
    });
533
});
534
535
describe('endsWith function', () => {
536
    it('should be true', () => {
537
        let fixtures = [
538
            'foo bar',
539
            'bar'
540
        ];
541
542
        fixtures.forEach(el => {
543
            chai.expect(endsWith(el, 'bar')).to.equal(true);
544
        });
545
546
        let fixtures2 = [
547
            'foo barr',
548
            'barr'
549
        ];
550
551
        fixtures2.forEach(el => {
552
            chai.expect(endsWith(el, 'bar', el.length-1)).to.equal(true);
553
        });
554
    });
555
});
556
557
describe('endsWith function caseSensitive', () => {
558
    it('should be true', () => {
559
        let fixtures = [
560
            'foo bar',
561
            'bar'
562
        ];
563
564
        fixtures.forEach(el => {
565
            chai.expect(endsWith(el, 'BAR', null, false)).to.equal(true);
566
        });
567
568
        let fixtures2 = [
569
            'foo barr',
570
            'barr'
571
        ];
572
573
        fixtures2.forEach(el => {
574
            chai.expect(endsWith(el, 'BAR', el.length-1, false)).to.equal(true);
575
        });
576
    });
577
});
578
579
describe('startsWith function', () => {
580
    it('should be true', () => {
581
        let fixtures = [
582
            'foo bar',
583
            'foobar',
584
            'foo'
585
        ];
586
587
        fixtures.forEach(el => {
588
            chai.expect(startsWith(el, 'foo')).to.equal(true);
589
        });
590
591
        let fixtures2 = [
592
            'afoo barr',
593
            'afoo'
594
        ];
595
596
        fixtures2.forEach(el => {
597
            chai.expect(startsWith(el, 'foo', 1)).to.equal(true);
598
        });
599
    });
600
});
601
602
describe('startsWith function caseSensitive', () => {
603
    it('should be true', () => {
604
        let fixtures = [
605
            'foo bar',
606
            'foobar',
607
            'foo'
608
        ];
609
610
        fixtures.forEach(el => {
611
            chai.expect(startsWith(el, 'FOO', 0, false)).to.equal(true);
612
        });
613
614
        let fixtures2 = [
615
            'afoo barr',
616
            'afoo'
617
        ];
618
619
        fixtures2.forEach(el => {
620
            chai.expect(startsWith(el, 'FOO', 1, false)).to.equal(true);
621
        });
622
    });
623
});
624
625
describe('ensureLeft function', () => {
626
    it('should be foobar', () => {
627
        let fixtures = [
628
            'bar',
629
            'foobar'
630
        ];
631
632
        fixtures.forEach(el => {
633
            chai.expect(ensureLeft(el, 'foo')).to.equal('foobar');
634
        });
635
    });
636
});
637
638
describe('ensureRight function', () => {
639
    it('should be foobar', () => {
640
        let fixtures = [
641
            'foo',
642
            'foobar'
643
        ];
644
645
        fixtures.forEach(el => {
646
            chai.expect(ensureRight(el, 'bar')).to.equal('foobar');
647
        });
648
    });
649
});
650
651
describe('first function', () => {
652
    it('should be foo', () => {
653
        let fixtures = [
654
            'foo',
655
            'foobar'
656
        ];
657
658
        fixtures.forEach(el => {
659
            chai.expect(first(el, 3)).to.equal('foo');
660
        });
661
    });
662
});
663
664
describe('last function', () => {
665
    it('should be foo', () => {
666
        let fixtures = [
667
            'foo',
668
            'foobarfoo'
669
        ];
670
671
        fixtures.forEach(el => {
672
            chai.expect(last(el, 3)).to.equal('foo');
673
        });
674
    });
675
});
676
677
describe('indexOf function', () => {
678
    it('should be true', () => {
679
        let value = 'foobar';
680
        chai.expect(indexOf(value, 'f')).to.equal(0);
681
        chai.expect(indexOf(value, 'o')).to.equal(1);
682
        chai.expect(indexOf(value, 'b')).to.equal(3);
683
        chai.expect(indexOf(value, 'a')).to.equal(4);
684
        chai.expect(indexOf(value, 'r')).to.equal(5);
685
        chai.expect(indexOf(value, 't')).to.equal(-1);
686
    });
687
});
688
689
describe('indexOf function caseSensitive', () => {
690
    it('should be true', () => {
691
        let value = 'FOOBAR';
692
        chai.expect(indexOf(value, 'f', undefined, false)).to.equal(0);
693
        chai.expect(indexOf(value, 'o', undefined, false)).to.equal(1);
694
        chai.expect(indexOf(value, 'b', undefined, false)).to.equal(3);
695
        chai.expect(indexOf(value, 'a', undefined, false)).to.equal(4);
696
        chai.expect(indexOf(value, 'r', undefined, false)).to.equal(5);
697
        chai.expect(indexOf(value, 't', undefined, false)).to.equal(-1);
698
    });
699
});
700
701
describe('lastIndexOf function', () => {
702
    it('should be true', () => {
703
        let value = 'foobarfoobar';
704
        chai.expect(lastIndexOf(value, 'f')).to.equal(6);
705
        chai.expect(lastIndexOf(value, 'o')).to.equal(8);
706
        chai.expect(lastIndexOf(value, 'b')).to.equal(9);
707
        chai.expect(lastIndexOf(value, 'a')).to.equal(10);
708
        chai.expect(lastIndexOf(value, 'r')).to.equal(11);
709
        chai.expect(lastIndexOf(value, 't')).to.equal(-1);
710
    });
711
});
712
713
describe('lastIndexOf function caseSensitive', () => {
714
    it('should be true', () => {
715
        let value = 'foobarfoobar';
716
        chai.expect(lastIndexOf(value, 'F', undefined, false)).to.equal(6);
717
        chai.expect(lastIndexOf(value, 'O', undefined, false)).to.equal(8);
718
        chai.expect(lastIndexOf(value, 'B', undefined, false)).to.equal(9);
719
        chai.expect(lastIndexOf(value, 'A', undefined, false)).to.equal(10);
720
        chai.expect(lastIndexOf(value, 'R', undefined, false)).to.equal(11);
721
        chai.expect(lastIndexOf(value, 'T', undefined, false)).to.equal(-1);
722
    });
723
});
724
725
describe('insert function', () => {
726
    it('should be foo bar', () => {
727
        chai.expect(insert('fbar', 'oo ', 1)).to.equal('foo bar');
728
        chai.expect(insert('foo', ' bar', 3)).to.equal('foo bar');
729
        chai.expect(insert('foo bar', 'asadasd', 13)).to.equal('foo bar');
730
    });
731
});
732
733
734
describe('length function', () => {
735
    it('should be 3', () => {
736
        chai.expect(length('foo')).to.equal(3);
737
        chai.expect(length('bar')).to.equal(3);
738
        chai.expect(length('dan')).to.equal(3);
739
    });
740
});
741
742
describe('leftPad function', () => {
743
    it('should be 00001', () => {
744
        chai.expect(leftPad('1', 5, 0)).to.equal('00001');
745
        chai.expect(leftPad('01', 5, 0)).to.equal('00001');
746
        chai.expect(leftPad('001', 5, 0)).to.equal('00001');
747
        chai.expect(leftPad('0001', 5, 0)).to.equal('00001');
748
        chai.expect(leftPad('00001', 5, 0)).to.equal('00001');
749
    });
750
});
751
752
describe('rightPad function', () => {
753
    it('should be 10000', () => {
754
        chai.expect(rightPad('1', 5, 0)).to.equal('10000');
755
        chai.expect(rightPad('10', 5, 0)).to.equal('10000');
756
        chai.expect(rightPad('100', 5, 0)).to.equal('10000');
757
        chai.expect(rightPad('1000', 5, 0)).to.equal('10000');
758
        chai.expect(rightPad('10000', 5, 0)).to.equal('10000');
759
    });
760
});
761
762
763
describe('removeLeft function', () => {
764
    it('should be true', () => {
765
        let fixtures = [
766
            'foobar',
767
            'bar'
768
        ];
769
770
        fixtures.forEach(el => {
771
            chai.expect(removeLeft(el, 'foo')).to.equal('bar');
772
        });
773
    });
774
});
775
776
describe('removeRight function', () => {
777
    it('should be true', () => {
778
        let fixtures = [
779
            'foobar',
780
            'foo'
781
        ];
782
783
        fixtures.forEach(el => {
784
            chai.expect(removeRight(el, 'bar')).to.equal('foo');
785
        });
786
787
        chai.expect(removeRight('foofoofoobar', 'bar')).to.equal('foofoofoo');
788
        chai.expect(removeRight('foofoofoobar', 'foobar')).to.equal('foofoo');
789
    });
790
});
791
792
describe('repeat function', () => {
793
    it('should be 1 repeated', () => {
794
        chai.expect(repeat('1', 1)).to.equal('1');
795
        chai.expect(repeat('1', 2)).to.equal('11');
796
        chai.expect(repeat('1', 3)).to.equal('111');
797
        chai.expect(repeat('1', 4)).to.equal('1111');
798
        chai.expect(repeat('1', 5)).to.equal('11111');
799
    });
800
});
801
802
describe('reverse function', () => {
803
    it('should be strings reverse', () => {
804
        chai.expect(reverse('foo')).to.equal('oof');
805
        chai.expect(reverse('daniel')).to.equal('leinad');
806
        chai.expect(reverse('')).to.equal('');
807
        chai.expect(reverse('bar')).to.equal('rab');
808
        chai.expect(reverse('foo_')).to.equal('_oof');
809
        chai.expect(reverse('f')).to.equal('f');
810
    });
811
});
812
813
describe('shuffle function', () => {
814
    it('should be strings shuffle', () => {
815
        chai.expect(length(shuffle('foo'))).to.equal(3);
816
        chai.expect(length(shuffle('daniel'))).to.equal(6);
817
        chai.expect(shuffle('')).to.equal('');
818
        chai.expect(shuffle('b')).to.equal('b');
819
    });
820
});
821
822
describe('surround function', () => {
823
    it('should be strings surround', () => {
824
        chai.expect(surround('foo', 'bar')).to.equal('barfoobar');
825
        chai.expect(surround('daniel', '_')).to.equal('_daniel_');
826
        chai.expect(surround('', '>')).to.equal('>>');
827
        chai.expect(surround('bar', '')).to.equal('bar');
828
        chai.expect(surround('f')).to.equal('f');
829
        chai.expect(surround('div','<','>')).to.equal('<div>');
830
    });
831
});
832
833
describe('safeTruncate function', () => {
834
    it('should be strings safeTruncated', () => {
835
        chai.expect(safeTruncate('foo bar', 0, '.')).to.equal('');
836
        chai.expect(safeTruncate('foo bar', 4, '.')).to.equal('foo.');
837
        chai.expect(safeTruncate('foo bar', 3, '.')).to.equal('.');
838
        chai.expect(safeTruncate('foo bar', 2, '.')).to.equal('.');
839
        chai.expect(safeTruncate('foo bar', 4, '.')).to.equal('foo.');
840
        chai.expect(safeTruncate('foo bar', 7, '.')).to.equal('foo bar');
841
        chai.expect(safeTruncate('foo bar', 8, '.')).to.equal('foo bar');
842
        chai.expect(safeTruncate('A Javascript string manipulation library.', 16, '...')).to.equal('A Javascript...');
843
        chai.expect(safeTruncate('A Javascript string manipulation library.', 15, '...')).to.equal('A Javascript...');
844
        chai.expect(safeTruncate('A Javascript string manipulation library.', 14, '...')).to.equal('A...');
845
    });
846
});
847
848
describe('truncate function', () => {
849
    it('should be strings truncated', () => {
850
        chai.expect(truncate('foo bar', 0, '.')).to.equal('');
851
        chai.expect(truncate('foo bar', 3, '.')).to.equal('fo.');
852
        chai.expect(truncate('foo bar', 2, '.')).to.equal('f.');
853
        chai.expect(truncate('foo bar', 4, '.')).to.equal('foo.');
854
        chai.expect(truncate('foo bar', 7, '.')).to.equal('foo bar');
855
        chai.expect(truncate('foo bar', 8, '.')).to.equal('foo bar');
856
    });
857
});
858
859
describe('format function', () => {
860
    it('should be formated strings', () => {
861
        chai.expect(format('foo bar')).to.equal('foo bar');
862
        chai.expect(format('{0} bar', ['foo'])).to.equal('foo bar');
863
        chai.expect(format('foo {0}', ['bar'])).to.equal('foo bar');
864
        chai.expect(format('foo {0}', ['bar', 'foo'])).to.equal('foo bar');
865
        chai.expect(format('{0} {1}', ['foo', 'bar'])).to.equal('foo bar');
866
        chai.expect(format('{1} {0}', ['bar', 'foo'])).to.equal('foo bar');
867
        chai.expect(format('{1} {0}', ['bar'])).to.equal('undefined bar');
868
        chai.expect(format('{foo} bar', {foo: 'foo'})).to.equal('foo bar');
869
        chai.expect(format('{foo} {bar}', {foo: 'foo', bar: 'bar'})).to.equal('foo bar');
870
    });
871
872
    it('should be undefined', () => {
873
        chai.expect(format('foo bar {0}')).to.equal('foo bar undefined');
874
    });
875
});
876
877
describe('removeEmptyStrings function', () => {
878
    it('should be [ \'aa\', \'bb\', \'cc\' ]', () => {
879
        chai.expect(removeEmptyStrings([ 'aa', '', 'bb', null, 'cc', undefined ])).to.deep.equal([ 'aa', 'bb', 'cc' ]);
880
    });
881
});
882
883
describe('compare function', () => {
884
    it('should be 1, -1, 0', () => {
885
        chai.expect(compare('a', 'b')).to.equal(-1);
886
        chai.expect(compare('b', 'a')).to.equal(1);
887
        chai.expect(compare('a', 'a')).to.equal(0);
888
        chai.expect(compare('0', '1')).to.equal(-1);
889
        chai.expect(compare('1', '0')).to.equal(1);
890
        chai.expect(compare('0', '0')).to.equal(0);
891
    });
892
});
893
894
describe('equal function', () => {
895
    it('should be true or false', () => {
896
        chai.expect(equal('a', 'b')).to.equal(false);
897
        chai.expect(equal('a', 'a')).to.equal(true);
898
        chai.expect(equal('0', 0)).to.equal(false);
899
    });
900
});
901
902
describe('inequal function', () => {
903
    it('should be true or false', () => {
904
        chai.expect(inequal('a', 'b')).to.equal(true);
905
        chai.expect(inequal('a', 'a')).to.equal(false);
906
        chai.expect(inequal('0', 0)).to.equal(true);
907
    });
908
});
909
910
describe('hexEncode function', () => {
911
    it('should be hexadecimal', () => {
912
        chai.expect(hexEncode('漢')).to.equal('6f22');
913
        chai.expect(hexEncode('A')).to.equal('0041');
914
        chai.expect(hexEncode('Á')).to.equal('00c1');
915
        chai.expect(hexEncode('AA')).to.equal('00410041');
916
    });
917
});
918
919
describe('hexDecode function', () => {
920
    it('should be string', () => {
921
        chai.expect(hexDecode('6f22')).to.equal('漢');
922
        chai.expect(hexDecode('0041')).to.equal('A');
923
        chai.expect(hexDecode('00c1')).to.equal('Á');
924
        chai.expect(hexDecode('00410041')).to.equal('AA');
925
    });
926
});
927
928
describe('binEncode function', () => {
929
    it('should be binary', () => {
930
        chai.expect(binEncode('漢')).to.equal('0110111100100010');
931
        chai.expect(binEncode('A')).to.equal('0000000001000001');
932
        chai.expect(binEncode('Á')).to.equal('0000000011000001');
933
        chai.expect(binEncode('AA')).to.equal('00000000010000010000000001000001');
934
    });
935
});
936
937
describe('binDecode function', () => {
938
    it('should be string', () => {
939
        chai.expect(binDecode('0110111100100010')).to.equal('漢');
940
        chai.expect(binDecode('0000000001000001')).to.equal('A');
941
        chai.expect(binDecode('0000000011000001')).to.equal('Á');
942
        chai.expect(binDecode('00000000010000010000000001000001')).to.equal('AA');
943
    });
944
});
945
946
describe('decEncode function', () => {
947
    it('should be binary', () => {
948
        chai.expect(decEncode('漢')).to.equal('28450');
949
        chai.expect(decEncode('A')).to.equal('00065');
950
        chai.expect(decEncode('Á')).to.equal('00193');
951
        chai.expect(decEncode('AA')).to.equal('0006500065');
952
    });
953
});
954
955
describe('decDecode function', () => {
956
    it('should be string', () => {
957
        chai.expect(decDecode('28450')).to.equal('漢');
958
        chai.expect(decDecode('00065')).to.equal('A');
959
        chai.expect(decDecode('00193')).to.equal('Á');
960
        chai.expect(decDecode('0006500065')).to.equal('AA');
961
    });
962
});
963
964
describe('base64Encode function', () => {
965
    it('should be string', () => {
966
        chai.expect(base64Encode('Daniel')).to.equal('RGFuaWVs');
967
        chai.expect(base64Encode('foo')).to.equal('Zm9v');
968
        chai.expect(base64Encode('bar')).to.equal('YmFy');
969
        chai.expect(base64Encode('bár!')).to.equal('YsOhciE=');
970
        chai.expect(base64Encode('漢')).to.equal('5ryi');
971
    });
972
});
973
974
describe('base64Decode function', () => {
975
    it('should be string', () => {
976
        chai.expect(base64Decode('RGFuaWVs')).to.equal('Daniel');
977
        chai.expect(base64Decode('Zm9v')).to.equal('foo');
978
        chai.expect(base64Decode('YmFy')).to.equal('bar');
979
        chai.expect(base64Decode('YsOhciE=')).to.equal('bár!');
980
        chai.expect(base64Decode('5ryi')).to.equal('漢');
981
    });
982
});
983
984
describe('htmlDecode function', () => {
985
    it('should be decoded html', () => {
986
        chai.expect(htmlDecode('&aacute;')).to.equal('\u00E1');
987
        chai.expect(htmlDecode('&SHcy;')).to.equal('Ш');
988
        chai.expect(htmlDecode('&ZHcy;')).to.equal('Ж');
989
        chai.expect(htmlDecode('&boxdl;')).to.equal('┐');
990
    });
991
});
992
993
describe('htmlEncode function', () => {
994
    it('should be encoded html', () => {
995
        chai.expect(htmlEncode('á')).to.equal('&aacute;');
996
        chai.expect(htmlEncode('áéíóú')).to.equal('&aacute;&eacute;&iacute;&oacute;&uacute;');
997
        chai.expect(htmlEncode('Ш')).to.equal('&SHcy;');
998
        chai.expect(htmlEncode('Ж')).to.equal('&ZHcy;');
999
        chai.expect(htmlEncode('┐')).to.equal('&boxdl;');
1000
    });
1001
});
1002